package danny.hoi.service.export.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import danny.hoi.dao.export.system.RoleDao;
import danny.hoi.domain.export.system.Role;
import danny.hoi.domain.export.system.RoleExample;
import danny.hoi.service.export.system.RoleService;
import danny.hoi.utils.UUUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll(RoleExample example) {
        return roleDao.selectByExample(example);
    }

    @Override
    public PageInfo<Role> findByPages(RoleExample example, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> roles = roleDao.selectByExample(example);

        return new PageInfo<>(roles);
    }

    @Override
    public Role findById(String id) {
        return roleDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Role role) {
        role.setRoleId(UUUIDUtil.getUuid());
        roleDao.insertSelective(role);
    }

    @Override
    public void update(Role role) {
        roleDao.updateByPrimaryKeySelective(role);
    }

    @Override
    public void delete(String id) {
        roleDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<Role> findRoleByUserId(String id) {
        return roleDao.findRoleByUserId(id);
    }

    @Override
    public void changeRole(String userId, String[] roleIds) {
        //删除原来的角色
        roleDao.deleteRoleByUserId(userId);
        //添加新的角色
        if (roleIds != null && roleIds.length > 0) {
            for (String roleId : roleIds) {
                roleDao.insertRoleUser(userId, roleId);
            }
        }
    }
}
